package main

import (
	"fmt"
	"log"
	"mysqlOp/course"
	"mysqlOp/init/mysql"
	"time"
)

func main() {
	mysql.InitDB()
	//insertCourse()
	//getCourse()
	//getCourseList()
	//updateCourse()
	//deleteCourse()
	insertCourseWithTeacher()
}

func insertCourse() {
	c := &course.Course{
		Name:       "C/C++ 服务器开发",
		Duration:   90,
		TeacherID:  0,
		Desc:       "C/C++ 服务器课程描述",
		CreateTime: time.Now(),
		UpdateTime: time.Now(),
	}
	c, err := course.Insert(c)
	if err != nil {
		log.Println(err)
		return
	}
	c1 := &course.Course{
		Name:       "golang 云原生",
		Duration:   90,
		TeacherID:  0,
		Desc:       "golang 云原生课程描述",
		CreateTime: time.Now(),
		UpdateTime: time.Now(),
	}
	c1, err = course.Insert(c1)
	if err != nil {
		log.Println(err)
		return
	}
	c2 := &course.Course{
		Name:       "音视频开发",
		Duration:   90,
		TeacherID:  0,
		Desc:       "音视频开发课程描述",
		CreateTime: time.Now(),
		UpdateTime: time.Now(),
	}
	c2, err = course.Insert(c2)
	if err != nil {
		log.Println(err)
		return
	}

	fmt.Printf("%+v\n", c)
	fmt.Printf("%+v\n", c1)
	fmt.Printf("%+v\n", c2)
}

func getCourse() {
	id := 2
	c, err := course.GetById(id)
	if err != nil {
		log.Println(err)
		return
	}
	fmt.Println(c)
}

func getCourseList() {
	maxID := 4
	list, err := course.GetList(maxID)
	if err != nil {
		log.Println(err)
		return
	}
	fmt.Println("查询列表结束")

	for _, l := range list {
		fmt.Println(l)
	}
	fmt.Println("查询列表结果")
}

func updateCourse() {
	c, _ := course.GetById(2)
	c.Name = "内核"
	c.Desc = "内核课程描述"
	c, err := course.Update(c)
	if err != nil {
		log.Println(err)
		return
	}
	fmt.Println(c)
}

func deleteCourse() {
	var id int64 = 3
	err := course.Delete(id)
	if err != nil {
		log.Println(err)
		return
	}
}

func insertCourseWithTeacher() {
	c := &course.Course{
		Name:       "golang 云原生",
		Duration:   90,
		TeacherID:  0,
		Desc:       "golang 云原生课程描述",
		CreateTime: time.Now(),
		UpdateTime: time.Now(),
	}
	c, err := course.InsertCourseWithTeacher(c, "Teacher alen")
	if err != nil {
		log.Println(err)
		return
	}
	fmt.Printf("%+v\n", c)
}
